#include <vector>
using namespace std;
class Solution {
public:
    /**
     * @param prices: Given an integer array
     * @return: Maximum profit
     */
    int maxProfit(vector<int> &prices) {
        // write your code here
        if(prices.size() == 0){ //时刻注意数组越界
            return 0;
        }
        
        int max = 0;
        int cur = prices[0];
        for(int i = 0; i < prices.size(); ++i){
            if(prices[i] < cur){ //卖掉会亏
                cur = prices[i];
            }else { //卖掉能挣
                int tmp = prices[i] - cur;
                if(tmp > max){
                    max = tmp;
                }
            }
        }
        return max;
    }
};
